package com.prp.gwt.client.widgets.entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.entry.EntryClientListener;

public class SignInPanel extends Composite {

	interface Binder extends UiBinder<Widget, SignInPanel> {
	}

	private static final Binder binder = GWT.create(Binder.class);
	private static SignInPanel instance;
	@UiField
	HTML signin;
	@UiField
	HTML register;
	@UiField
	HTML forget;
	@UiField
	TextBox email = new TextBox();
	@UiField
	PasswordTextBox password = new PasswordTextBox();

	public static SignInPanel getInstance() {
		if(instance == null)
			instance = new SignInPanel();
		return instance;
	}

	private SignInPanel() {
		initWidget(binder.createAndBindUi(this));
		signin.setHTML("<a>登录进站</a>");
		register.setHTML("<a>新用户注册</a>");
		forget.setHTML("<a>忘记密码</a>");
	}

	@UiHandler("password")
	public void onPasswordKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == 13)
			EntryClientListener.getInstance().signIn(email.getText().trim(),
					password.getText());
	}

	@UiHandler("signin")
	public void onSigninClick(ClickEvent event) {
		EntryClientListener.getInstance().signIn(email.getText().trim(),
				password.getText());
	}

	@UiHandler("register")
	public void onRegisterClick(ClickEvent event) {
		Window.Location.replace(Host.getREGISTER());
	}

	@UiHandler("forget")
	public void onForgetClick(ClickEvent event) {
		WelcomeView.getInstance().showWidget(1);
	}

}
